package com.suxin.netty.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author Tang
 * @classname ProtocolMessageToByteEncoder
 * @description [ 编码器 ]
 * @date 2021/7/18 11:30
 */
public class ProtocolMessageToByteEncoder extends MessageToByteEncoder<ProtocolMsg> {

    @Override
    protected void encode(ChannelHandlerContext ctx, ProtocolMsg msg, ByteBuf out) throws Exception {
        if (msg.getLen() > 0) {
            out.writeInt(msg.getLen());
            out.writeBytes(msg.getData());
        }
    }
}